/*
 * main.c - This file contains the main program and driver for the raytracer.
 *
 *  $Id: main.c,v 1.75 2009/05/16 19:31:45 johns Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../src/tachyon.h"    /* The Tachyon ray tracing library API */
#include "../src/measures/measuring.h" /* para las medidas */
#include "getargs.h"    /* command line argument/option parsing */
#include "parse.h"      /* Support for my own scene file format */
#include "nffparse.h"   /* Support for NFF files, as in SPD */
#include "ac3dparse.h"  /* Support for AC3D files */
#include "mgfparse.h"   /* Support for MGF files */


typedef struct {
  float x;
  float y;
  float z;
} floatvec;

static void my_ui_message(int a, char * msg) {
  printf("%s\n", msg);
}

static void my_ui_progress(int percent) {
  printf("\rRendering Progress:       %3d%% complete            \r", percent);
  fflush(stdout);
}


int main(int argc, char **argv) {
  SceneHandle scene;
  unsigned int rc;
  argoptions opt;
  char * filename;
  int node, fileindex;
  rt_timerhandle parsetimer;

  node = rt_initialize(&argc, &argv);

  rt_set_ui_message(my_ui_message);
  rt_set_ui_progress(my_ui_progress);

  if (node == 0) {
	printf("IRRADIA IA64 Parallel Ray Tracer   based in:   \n");
    printf("Tachyon Parallel/Multiprocessor Ray Tracer   Version %s   \n", TACHYON_VERSION_STRING);
    printf("Copyright 1994-2009,    John E. Stone <john.stone@gmail.com> \n");
    printf("------------------------------------------------------------ \n");
  }

  if ((rc = getargs(argc, argv, &opt, node)) != 0) {
    rt_finalize();
    exit(rc);
  }

  if (opt.numfiles > 1) {
    printf("Rendering %d scene files.\n", opt.numfiles);
  }

  for (fileindex=0; fileindex<opt.numfiles; fileindex++) {
    scene = rt_newscene();


    /* process command line overrides */
    presceneoptions(&opt, scene);

    filename = opt.filenames[fileindex];

    if (opt.numfiles > 1) {
      printf("\nRendering scene file %d of %d, %s\n", fileindex+1, opt.numfiles, filename);
    }

    parsetimer=rt_timer_create();
    rt_timer_start(parsetimer);

    if (strstr(filename, ".nff") || strstr(filename, ".NFF")) {
      rc = ParseNFF(filename, scene); /* must be an NFF file */
    }
    else if (strstr(filename, ".ac") || strstr(filename, ".AC")) {
      rc = ParseAC3D(filename, scene); /* Must be an AC3D file */
    }
#ifdef USELIBMGF
    else if (strstr(filename, ".mgf") || strstr(filename, ".MGF")) {
      rc = ParseMGF(filename, scene, 1); /* Must be an MGF file */
    }
#endif
    else {
      rc = readmodel(filename, scene); /* Assume its a Tachyon scene file */
    }

    rt_timer_stop(parsetimer);
    if (rc == PARSENOERR && node == 0)
      printf("Scene Parsing Time: %10.4f seconds\n", rt_timer_time(parsetimer));
    rt_timer_destroy(parsetimer);

    if (rc != PARSENOERR && node == 0) {
      switch(rc) {
        case PARSEBADFILE:
          printf("Parser failed due to nonexistent input file: %s\n", filename);
          break;
        case PARSEBADSUBFILE:
          printf("Parser failed due to nonexistent included file.\n");
          break;
        case PARSEBADSYNTAX:
          printf("Parser failed due to an input file syntax error.\n");
          break;
        case PARSEEOF:
          printf("Parser unexpectedly hit an end of file.\n");
          break;
        case PARSEALLOCERR:
          printf("Parser ran out of memory.\n");
          break;
      }
      if (fileindex+1 < opt.numfiles)
        printf("Aborting render, continuing with next scene file...\n");
      else
        printf("Aborting render.\n");

      rt_deletescene(scene); /* free the scene */
      continue;              /* process the next scene */
    }

    /* process command line overrides */
    postsceneoptions(&opt, scene);

    /* choose which rendering mode to use */
    if (opt.usecamfile == 1) {
    	/* modo camara no soportado
      return animate_scene(opt, scene, node);  fly using prerecorded data */
    }
    else {
      if (opt.numfiles > 1 && opt.nosave != 1) {
        char multioutfilename[FILENAME_MAX];
        sprintf(multioutfilename, opt.outfilename, fileindex);
        rt_outputfile(scene, multioutfilename);
      }

      rt_renderscene(scene); /* Render a single frame */
    }

    rt_deletescene(scene);   /* free the scene, get ready for next one */
  }

  rt_finalize();             /* close down the rendering library and MPI */
  freeoptions(&opt);         /* free parsed command line option data */

  return 0;
}


